package com.fdb.efp.nls.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fdb.basic.framework.core.base.AbstractFrameworkService;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.constant.SequenceConstance;
import com.fdb.basic.framework.core.exception.BizException;
import com.fdb.basic.framework.core.exception.PauseException;
import com.fdb.basic.framework.core.util.DateTool;
import com.fdb.basic.framework.core.util.ReflectUtility;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.nls.service.facade.PubSysInfoService;
import com.fdb.efp.nls.common.ContNoUtil;
import com.fdb.efp.nls.service.facade.CtrLoanContService;
import com.fdb.efp.nls.service.vo.CtrLoanContVO;
import com.fdb.efp.cus.service.vo.CusIndivVO;
import com.fdb.efp.nls.common.LoanConstant;
import com.fdb.efp.nls.service.dao.AccLoanDao;
import com.fdb.efp.nls.service.domain.AccLoan;
import com.fdb.efp.nls.service.facade.AccLoanService;
import com.fdb.efp.nls.service.vo.AccLoanStatisticalInfo;
import com.fdb.efp.nls.service.vo.AccLoanVO;
import com.fdb.efp.nls.service.vo.RscAdjustBillVO;
import com.fdb.efp.nls.service.vo.RscAdjustContVO;
import com.fdb.efp.nls.service.vo.RscRemoveBillVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 * ServiceImpl: 贷款台账
 * <p>
 *
 * @author chenqm1
 *
 * @date 2018-05-03
 */
@Service("accLoanService")
public class AccLoanServiceImpl extends AbstractFrameworkService implements AccLoanService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(AccLoanServiceImpl.class);

	@Autowired
	private AccLoanDao accLoanDao;

	@Autowired
	@Qualifier("ctrLoanContService")
	private CtrLoanContService ctrLoanContService;

	@Autowired
	@Qualifier("pubSysInfoService")
	private PubSysInfoService pubSysInfoService;

	/**
	 * 新增操作
	 */
	@Override
	public int insertAccLoan(AccLoanVO accLoanVo) throws Exception {
		logger.debug("当前新增数据为:" + accLoanVo.toString());
		int num = 0;
		try {
			AccLoan accLoan = new AccLoan();
			beanCopy(accLoanVo, accLoan);
			num = accLoanDao.insertAccLoan(accLoan);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 新增操作
	 */
	@Override
	public String insertAccLoanByBizSerno(String pvpSerno) throws PauseException {
		logger.info("新增数据为传入出账信息表流水号为:" + pvpSerno);
		String loanAccount = "";
//		BizFlowExeProcessVO exeProcess = null;
		int finishStep = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 查询出账信息
//			PvpLoanAppVO pla = new PvpLoanAppVO();
//			pla.setBizSerno(pvpSerno);//
//			PvpLoanAppVO pvpLoanApp = pvpLoanAppService.queryByPk(pla);
//			if (Objects.isNull(pvpLoanApp)) {
//				logger.info("根据流水号" + pvpSerno + "查询出账信息为空!");
//				throw new PauseException("根据流水号" + pvpSerno + "查询出账信息为空!");
//			}

//			exeProcess = queryBizFlowExeProcessByBizSerno(pvpLoanApp.getContNo());
//			finishStep = exeProcess.getFinishSteps();// 已完成步骤
			// 如果已经处理完成，直接返回
//			if (BaseConstant.YES.equals(exeProcess.getFinishInd())) {
//				map = JSON.parseObject(exeProcess.getResultParam());
//				return (String) map.get("acctNo");
//			}

			CtrLoanContVO vo = new CtrLoanContVO();
//			vo.setContNo(pvpLoanApp.getContNo());
			CtrLoanContVO ctrLoanContVo = ctrLoanContService.queryByPk(vo);
//			if (Objects.isNull(ctrLoanContVo)) {
//				logger.info("根据合同号" + pvpLoanApp.getContNo() + "查询合同信息为空!");
//				throw new PauseException("根据合同号" + pvpLoanApp.getContNo() + "查询合同信息为空!");
//			}
//			PrdInfoVO prdInfoVo = new PrdInfoVO();
//			prdInfoVo.setPrdId(pvpLoanApp.getPrdId());
//			PrdInfoVO prdInfo = null;// prdInfoService.queryByPk(prdInfoVo);
//			if (Objects.isNull(prdInfo)) {
//				logger.info("根据产品号" + pvpLoanApp.getPrdId() + "查询产品信息为空!");
//				throw new PauseException("根据产品号" + pvpLoanApp.getPrdId() + "查询产品信息为空!");
//			}
			// 封装台帐信息
			AccLoan accLoan = new AccLoan();
			String billNo = this.getSequence(SequenceConstance.LOAN_BILL_SEQ_ID);
			billNo += ContNoUtil.getCheckDigitHbs(billNo);// 校验位，与合同号校验位规则一致

//			beanCopy(pvpLoanApp, accLoan);

			accLoan.setCertType(ctrLoanContVo.getCertType());// 证件类型
			accLoan.setCertCode(ctrLoanContVo.getCertCode());// 证件号码
			accLoan.setLmtContNo(ctrLoanContVo.getLmtContNo());// 授信协议编号
//			accLoan.setCyclicWay(prdInfo.getCyclicWay());// 额度是否循环
			accLoan.setIsUpload(ctrLoanContVo.getUploadDocInd());// 是否需要上传资料
			accLoan.setBillNo(billNo);
			accLoan.setLoanBalance(BigDecimal.ZERO);// 贷款余额
			accLoan.setNormalBalance(BigDecimal.ZERO);// 正常贷款余额
//			accLoan.setTermType(DataTradeUtil.calTermType(pvpLoanApp.getLoanStartDate(), pvpLoanApp.getLoanEndDate()));
			accLoan.setDefaultFlag(BaseConstant.NO);// 违约标识
			accLoan.setExtensionTimes(BigDecimal.ZERO);// 展期次数
			accLoan.setOverTimesCurrent(BigDecimal.ZERO);// 当前逾期期数
			accLoan.setOverTimesTotal(BigDecimal.ZERO);// 累计逾期期数
			accLoan.setMaxTimesTotal(BigDecimal.ZERO);// 最高逾期期数
			accLoan.setAccountStatus(LoanConstant.TZZT_WQR);// 台账状态
			accLoan.setBadLoanFlag(BaseConstant.NO);// 转不良标志

			accLoan.setReceIntCumu(BigDecimal.ZERO); // 应收利息累计
			accLoan.setActualIntCumu(BigDecimal.ZERO); // 实收利息累计
			accLoan.setActArrsIntBal(BigDecimal.ZERO); // 实收拖欠本金的罚息累计
			accLoan.setActArrPrnBal(BigDecimal.ZERO); // 实收复利累计
			accLoan.setUnpdIntArrPrn(BigDecimal.ZERO); // 应收利息的罚息(应收未收)累计
			accLoan.setUnpdArrsIntBal(BigDecimal.ZERO); // 应收拖欠本金的罚息(应收未收)累计
			accLoan.setUnpdArrPrnBal(BigDecimal.ZERO); // 应收复利(应收未收)累计
			accLoan.setActIntArrPrn(BigDecimal.ZERO); // 实收利息的罚息累计
			accLoan.setIscircle(BaseConstant.YES);// 是否自助循环贷款
			accLoan.setCla(LoanConstant.FIVE_SORT_10);// 五级风险分类,送省信贷文件必输
			// 调用30022000007 012000_1 获取贷款账号和会计日期
			String acctDate = "";
//			if (finishStep == 0) {
//				try {
//					logger.info("开始调用贷款开户接口30022000007，出账流水号：" + pvpSerno + "，借据号：" + billNo);
//					map = new HashMap<>();// PICCSToBANCSService.getAccountInfo(pvpSerno,billNo);
//					loanAccount = (String) map.get("acctNo");
//					acctDate = (String) map.get("txnDt");
//					logger.info("获取贷款账号为：" + loanAccount);
//					++finishStep;
//					exeProcess.setResultParam(JSONObject.toJSONString(map));// 执行结果参数
//				} catch (Exception e) {
//					throw new PauseException("获取贷款账号和会计日期出错!");
//				}
//			} else {
//				map = JSON.parseObject(exeProcess.getResultParam());
//				loanAccount = (String) map.get("acctNo");
//				acctDate = (String) map.get("txnDt");
//			}
			accLoan.setLoanAccount(loanAccount);// 贷款账号
			accLoan.setAcctDate(DateTool.formatDate(acctDate, DateTool.PATTERN_DEFAULT));
			accLoan.setFirstDisbDate(DateTool.formatDate(acctDate, DateTool.PATTERN_DEFAULT));// 首次放款日期
			accLoan.setLoanStartDate("1899-12-31");// 贷款起始日
			accLoan.setCreateTime(TimeUtil.getCurrentDateTime());// 创建时间
			accLoan.setLastUpdateTime(TimeUtil.getCurrentDateTime());// 修改时间
			accLoanDao.insertAccLoan(accLoan);
			++finishStep;
		} catch (Exception e) {
			logger.error("插入台账数据出错!", e);
			throw new PauseException("插入台账数据出错!");
		} finally {
//			if (Objects.nonNull(exeProcess)) {
//				exeProcess.setFinishSteps(finishStep);// 已完成步骤数
//				exeProcess.setTotalSteps(2);// 总步骤数
//				exeProcess.setFinishInd(exeProcess.getFinishSteps() == exeProcess.getTotalSteps() ? BaseConstant.YES : BaseConstant.NO);// 是否已完成
//				exeProcess.setLastUpdateTime(TimeUtil.getCurrentDateTime());// 最后修改时间
//
//				// bizFlowExeProcessService.updateBizFlowExeProcessByPk(exeProcess);
//			}
		}
		return loanAccount;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(AccLoanVO accLoanVo) {
		logger.debug("当前删除数据条件为:" + accLoanVo);
		int num = 0;
		try {
			AccLoan accLoan = new AccLoan();
			beanCopy(accLoanVo, accLoan);
			num = accLoanDao.deleteByPk(accLoan);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + accLoanVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(AccLoanVO accLoanVo) {
		logger.debug("当前修改数据为:" + accLoanVo.toString());
		int num = 0;
		try {
			AccLoan accLoan = new AccLoan();
			beanCopy(accLoanVo, accLoan);
			num = accLoanDao.updateByPk(accLoan);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + accLoanVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 批量更新 <br>
	 * @author dengqiwen
	 * @date 2020年10月14日
	 * @version 0.1
	 */
	@Override
	public int batchUpdate(List<AccLoanVO> updateList) {
		logger.debug("当前修改数据为:" + updateList.size() + "条");
		List<AccLoan> accLoanList = new ArrayList<AccLoan>();
		int num = 0;
		try {
			accLoanList = (List<AccLoan>) beansCopy(updateList, AccLoan.class);
			num = accLoanDao.batchUpdate(accLoanList);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public AccLoanVO queryByPk(AccLoanVO accLoanVo) {

		logger.debug("当前查询参数信息为:" + accLoanVo);
		try {
			AccLoan queryAccLoan = new AccLoan();
			beanCopy(accLoanVo, queryAccLoan);
			AccLoan queryRslAccLoan = accLoanDao.queryByPk(queryAccLoan);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 主动催收申请表
	 *
	 * @param accLoanVO
	 * @return
	 */

	@Override
	public List<AccLoanVO> queryColtAutoApp(AccLoanVO accLoanVO) {
		logger.debug("主动催收申请表的参数信息为:");
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryColtAutoApp(accLoanVO);
			logger.debug("主动催收申请表的结果集数量为:" + accLoans.size());
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<AccLoanVO> queryAllOwner(AccLoanVO accLoanVo) {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryAllOwnerByPage(accLoanVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + accLoans.size());
			pageSet(accLoans, accLoanVo);
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<AccLoanVO> queryAllCurrOrg(AccLoanVO accLoanVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		AccLoan accLoan = beanCopy(accLoanVo, new AccLoan());
		List<AccLoan> accLoans = accLoanDao.queryAllCurrOrgByPage(accLoanVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + accLoans.size());
		List<AccLoanVO> list = null;
		try {
			pageSet(accLoans, accLoanVo);
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<AccLoanVO> queryAllCurrDownOrg(AccLoanVO accLoanVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		AccLoan accLoan = beanCopy(accLoanVo, new AccLoan());
		List<AccLoan> accLoans = accLoanDao.queryAllCurrDownOrgByPage(accLoanVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + accLoans.size());
		List<AccLoanVO> list = null;
		try {
			pageSet(accLoans, accLoanVo);
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@Override
	@SuppressWarnings("unchecked")
	public List<AccLoanVO> queryAllCurrOwnerPrd(AccLoanVO accLoanVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		AccLoan accLoan = beanCopy(accLoanVo, new AccLoan());
		List<AccLoan> accLoans = accLoanDao.queryAllCurrOwnerPrdByPage(accLoanVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + accLoans.size());
		List<AccLoanVO> list = null;
		try {
			pageSet(accLoans, accLoanVo);
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}
	@Override
	public AccLoanVO querySigleAccLoanByCondition(AccLoanVO accLoanVo) {
		logger.debug("当前查询参数信息为:" + accLoanVo);
		try {
			AccLoan queryAccLoan = new AccLoan();
			beanCopy(accLoanVo, queryAccLoan);
			AccLoan queryRslAccLoan = accLoanDao.querySigleAccLoanByCondition(queryAccLoan);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public List<AccLoanVO> queryListByCont(AccLoanVO accLoanVo) {

		logger.debug("当前查询合同底下所属借据信息的参数信息为:");
		AccLoan accLoan = beanCopy(accLoanVo, new AccLoan());
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryAllByCont(accLoanVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + accLoans.size());
			pageSet(accLoans, accLoan);
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccLoanVO> queryAllContNo(String contNo) {
		logger.info("查询合同底下所属借据信息的参数信息为:" + contNo);
		List<AccLoanVO> lists = null;
		try {
			List<AccLoan> list = accLoanDao.queryAllContNo(contNo);
			overdueDays(list);
			logger.info("查询合同底下所属借据信息数量为:" + list.size());
			lists = (List<AccLoanVO>) beansCopy(list, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return lists;
	}

	@Override
	public AccLoanVO queryBillNoByLoanAccount(AccLoanVO accLoanVo) {
		AccLoan accLoan = beanCopy(accLoanVo, new AccLoan());
		AccLoanVO outAccLoanVo = new AccLoanVO();

		AccLoan accLoan2 = accLoanDao.queryBillNoByLoanAccount(accLoan);

		outAccLoanVo = beanCopy(accLoan2, outAccLoanVo);
		return outAccLoanVo;
	}

	@Override
	public List<AccLoanVO> queryListByCusType(String cusType) {
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = null;
			if ("130".equals(cusType)) {
				accLoans = accLoanDao.queryListFatmer();
			} else {
				accLoans = accLoanDao.queryListOther();
			}
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + accLoans.size());
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@Override
	public AccLoanVO queryByManualIdt(AccLoanVO accLoanVo) {

		logger.debug("当前查询参数信息为:" + accLoanVo);
		try {
			AccLoan queryAccLoan = new AccLoan();
			beanCopy(accLoanVo, queryAccLoan);
			AccLoan queryRslAccLoan = accLoanDao.queryByManualIdt(queryAccLoan);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public AccLoanVO queryBySevenResult(AccLoanVO accLoanVo) {

		logger.debug("当前查询参数信息为:" + accLoanVo);
		try {
			AccLoan queryAccLoan = new AccLoan();
			beanCopy(accLoanVo, queryAccLoan);
			AccLoan queryRslAccLoan = accLoanDao.queryBySevenResult(queryAccLoan);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public List<AccLoanVO> queryListByAccStatus(AccLoanVO accLoanVo) {
		List<AccLoanVO> list = null;
		try {
			AccLoan accLoan = new AccLoan();
			beanCopy(accLoanVo, accLoan);
			List<AccLoan> accLoans = accLoanDao.queryListByAccStatus(accLoan);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + accLoans.size());
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public List<String> queryContListByAccStatus() {
		List<String> contList = accLoanDao.queryContListByAccStatus();
		logger.debug("当前查询未结清合同号的结果集数量为:" + contList.size());
		return contList;
	}

	/**
	 * 未结清借据进行查询
	 */
	@Override
	public List<AccLoanVO> queryOverdueDays(AccLoanVO accLoanVO) {
		logger.info("未结清借据进行查询参数为:" + JSONObject.toJSONString(accLoanVO));
		List<AccLoanVO> lists = null;
		if (!allFieldsIsNull(accLoanVO)) {
			try {
				List<AccLoan> list = accLoanDao.queryOverdueDaysByPage(accLoanVO);
				overdueDays(list);
				logger.info("查询未结清借据数量为:" + list.size());
				pageSet(list, accLoanVO);
				lists = (List<AccLoanVO>) beansCopy(list, AccLoanVO.class);
			} catch (Exception e) {
				logger.error("数据转换出现异常!", e);
			}
		}
		return lists;
	}

	private void overdueDays(List<AccLoan> list) throws Exception {
		logger.info("开始调用overdueDays方法" + JSONObject.toJSONString(list));
		for (int i = 0; i < list.size(); i++) {
			AccLoan accLoans = list.get(i);
			String capOverdueDate = accLoans.getCapOverdueDate();
			String intOverdueDate = accLoans.getIntOverdueDate();
			logger.info("本金逾期起始日：" + capOverdueDate + "，利息逾期起始日:" + intOverdueDate);
			if ((capOverdueDate != null && !"".equals(capOverdueDate)) || (intOverdueDate != null && !"".equals(intOverdueDate))) {
				String openDay = pubSysInfoService.getOpenday(BaseConstant.SYS_ID_1001);
				logger.info("当前营业日期为：" + openDay);
				int num = 0;
				if ((capOverdueDate != null && !"".equals(capOverdueDate)) && (intOverdueDate == null || "".equals(intOverdueDate))) {
					num = TimeUtil.getDaysBetweenTwoDates(capOverdueDate, openDay);
					logger.info("本金逾期起始日不为空，与当前营业日期相差天数：" + num);
					accLoans.setOverdueDays(new BigDecimal(num));
				} else if ((capOverdueDate == null || "".equals(capOverdueDate))
						&& (intOverdueDate != null && !"".equals(intOverdueDate))) {
					num = TimeUtil.getDaysBetweenTwoDates(intOverdueDate, openDay);
					logger.info("利息逾期起始日不为空，与当前营业日期相差天数：" + num);
					accLoans.setOverdueDays(new BigDecimal(num));
				} else {

					int result = TimeUtil.compareDates(intOverdueDate, capOverdueDate);
					logger.info("比较本金逾期起始日和利息逾期起始日大小，结果为：" + result);
					if (result == 1) {
						num = TimeUtil.getDaysBetweenTwoDates(capOverdueDate, openDay);
					} else if (result == -1) {
						num = TimeUtil.getDaysBetweenTwoDates(intOverdueDate, openDay);
					} else if (result == 0) {
						num = TimeUtil.getDaysBetweenTwoDates(intOverdueDate, openDay);
					}
					logger.info("相差天数" + num);
					accLoans.setOverdueDays(new BigDecimal(num));
				}
			} else {
				logger.info("本金逾期起始日和利息逾期起始日都为空，逾期天数为0");
				accLoans.setOverdueDays(new BigDecimal(0));
			}
			CusIndivVO cusIndivVo = new CusIndivVO();
			cusIndivVo.setCusId(accLoans.getCusId());
			CusIndivVO cusIndiv = new CusIndivVO();// cusIndivService.queryByPk(cusIndivVo);
			if (Objects.nonNull(cusIndiv) && cusIndiv != null) {
				logger.info("客户信息为：" + JSONObject.toJSONString(cusIndiv));
				String crdGrade = cusIndiv.getCrdGrade();
				if (crdGrade != null && !"".equals(crdGrade)) {
					accLoans.setCreditGrade(crdGrade);
					accLoans.setGuarStatus("无异常");
					// accLoans.setAutoResult(accLoans.getAutoResult());
					accLoans.setPreAdjustResult(accLoans.getSevenResult());
				} else {
					accLoans.setGuarStatus("无异常");
					// accLoans.setAutoResult(accLoans.getAutoResult());
					accLoans.setPreAdjustResult(accLoans.getSevenResult());
				}
				logger.info("台账信息为：" + JSONObject.toJSONString(accLoans));
			}
		}
	}

	// 风险分类调整解除审批通过后更新台账表
	@Override
	public boolean batchUpdateAccLoan(String taskNo) throws PauseException {
		logger.info("风险分类调整解除审批通过后更新台账表入参：" + taskNo);
		RscRemoveBillVO rscRemoveBillVo = new RscRemoveBillVO();
		rscRemoveBillVo.setTaskNo(taskNo);
		boolean result = false;
		int num = 0;
		try {
			List<RscRemoveBillVO> list = new ArrayList<RscRemoveBillVO>();// rscRemoveBillService.queryAllOwner(rscRemoveBillVo);
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					RscRemoveBillVO rscRemoveBill = list.get(i);
					AccLoan inVo = new AccLoan();
					inVo.setBillNo(rscRemoveBill.getBillNo());
					AccLoan accLoanVo = accLoanDao.queryByPk(inVo);
					accLoanVo.setSevenResultPre(accLoanVo.getSevenResult());
					accLoanVo.setSevenResult(accLoanVo.getAutoResult());
					accLoanVo.setManualIdt(BaseConstant.NO);
					accLoanVo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
					logger.info("更新台账表accLoanVo参数为：!" + JSONObject.toJSONString(accLoanVo));
					num = accLoanDao.updateByPk(accLoanVo);
					if (num != 1) {
						logger.info("风险分类调整解除审批通过后更新台账表出错!");
						break;
					}
					result = true;
				}
			}

		} catch (Exception e) {
			logger.error("解除风险分类调整审批通过更新台账表出错!", e);
			throw new PauseException("解除风险分类调整审批通过更新台账表出错!");
		}
		return result;
	}

	@Override
	public boolean batchUpdateAccLoanForAdjustBill(String taskNo) throws PauseException {
		// 按借据发起风险分类调整审批通过后更新台账表
		logger.info("按借据发起风险分类调整审批通过后更新台账表入参：" + taskNo);
		RscAdjustBillVO rscAdjustBillvo = new RscAdjustBillVO();
		rscAdjustBillvo.setTaskNo(taskNo);
		boolean result = false;
		int num = 0;
		try {
			List<RscAdjustBillVO> list = new ArrayList<RscAdjustBillVO>();// rscAdjustBillService.queryAllOwner(rscAdjustBillvo);
			for (int i = 0; i < list.size(); i++) {
				RscAdjustBillVO rscAdjustBillVO = list.get(i);
				AccLoan inVo = new AccLoan();
				inVo.setBillNo(rscAdjustBillVO.getBillNo());
				AccLoan accLoanVo = accLoanDao.queryByPk(inVo);
				accLoanVo.setSevenResultPre(accLoanVo.getSevenResult());
				accLoanVo.setSevenResult(rscAdjustBillVO.getAdjustResult());
				accLoanVo.setManualIdt(BaseConstant.YES);
				accLoanVo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
				logger.info("按借据发起风险分类调整审批通过后更新台账表入参：" + JSONObject.toJSONString(accLoanVo));
				num = accLoanDao.updateByPk(accLoanVo);
				if (num != 1) {
					logger.info("按借据维度发起风险分类调整审批通过后更新台账表出错!");
					break;
				}
				result = true;
			}
		} catch (Exception e) {
			logger.error("按借据维度发起风险分类调整审批通过流程出错!", e);
			throw new PauseException("按借据维度发起风险分类调整审批通过流程出错!");

		}
		return result;
	}

	@Override
	public boolean batchUpdateAccLoanForAdjustCont(String taskNo) throws PauseException {
		logger.info("按合同发起风险分类调整审批通过后入参：" + taskNo);
		boolean result = false;
		try {
			RscAdjustContVO rscAdjustContVo = new RscAdjustContVO();
			rscAdjustContVo.setTaskNo(taskNo);
			List<RscAdjustContVO> list = new ArrayList<RscAdjustContVO>();// rscAdjustContService.queryAllOwner(rscAdjustContVo);
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					RscAdjustContVO rscAdjustCont = list.get(i);
					String contNo = rscAdjustCont.getContNo();
					List<AccLoan> listBill = accLoanDao.queryAllContNo(contNo);
					for (int b = 0; b < listBill.size(); b++) {
						AccLoan accLoan = listBill.get(b);
						accLoan.setSevenResultPre(accLoan.getSevenResult());
						accLoan.setSevenResult(rscAdjustCont.getAdjustResult());
						accLoan.setManualIdt(BaseConstant.YES);
						accLoan.setLastUpdateTime(TimeUtil.getCurrentDateTime());
						logger.info("按合同发起风险分类调整审批通过后更新台账表入参：" + JSONObject.toJSONString(accLoan));
						accLoanDao.updateByPk(accLoan);
					}
				}
			}
			result = true;
		} catch (Exception e) {
			logger.error("按合同维度发起风险分类调整审批通过流程出错!", e);
			throw new PauseException("按合同维度发起风险分类调整审批通过流程出错!");
		}
		return result;
	}

	@Override
	public List<AccLoanVO> queryAccLoanByContNos(List<String> contNos) {
		if (null == contNos || contNos.size() == 0)
			return null;
		List<AccLoanVO> list = null;
		try {
			list = accLoanDao.queryAccLoanByContNos(contNos);
			if (null != list && list.size() > 0)
				list.get(0).setTotal(list.size());

		} catch (Exception e) {
			logger.error("queryAccLoanByContNos,查询数据出现异常!", e);
		}

		return list;
	}

	@Override
	public List<AccLoanVO> queryCusIdListByAccStatus() {
		List<AccLoan> accLoans = accLoanDao.queryCusIdListByAccStatus();
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + accLoans.size());
		List<AccLoanVO> list = null;
		try {
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	/**
	 * 根据法人机构号查询 该法人下放款总额度
	 */
	@Override
	public BigDecimal queryLoanBalanceByLegalOrgCode(String legalOrgCode) {
		AccLoan accLoan = new AccLoan();
		accLoan.setLegalOrgCode(legalOrgCode);
		try {
			logger.info("查询目前下放款额度法人机构号为: " + legalOrgCode);
			BigDecimal loanBalance = accLoanDao.queryLoanBalanceByLegalOrgCode(accLoan);
			logger.info("查询法人机构: " + legalOrgCode + " 下的目前放款额度为: " + loanBalance);
			return loanBalance;
		} catch (Exception e) {
			logger.error("查询当前放款总额度出错", e);
		}
		return null;
	}

	// 判断前端是否有参数传入，进行条件搜索
	public static boolean allFieldsIsNull(Object obj) {
		if (null == obj)
			return true;
		Field[] fields = obj.getClass().getDeclaredFields();
		Object value;
		for (int i = 0; i < fields.length; i++) {
			try {
				value = ReflectUtility.getObjectFieldValue(obj, fields[i]);
				if (value == null)
					continue;
				if (value instanceof String) {
					String s = (String) value;
					if ("".equals(s.trim()))
						continue;
				}
				if (value instanceof List) {
					List<?> list = (List<?>) value;
					if (list.size() == 0)
						continue;
					int num = 0; // 空值的次数
					for (Object object : list) {
						if (object == null)
							num++;
						if (object instanceof String) {
							String s = (String) object;
							if ("".equals(s.trim()))
								num++;
						}
					}
					if (num == list.size())
						continue;
				}
				if (value.getClass().isPrimitive())
					continue;
				if ("serialVersionUID".equals(fields[i].getName()))
					continue;
				return false;
			} catch (IllegalArgumentException e) {
			}
		}
		return true;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<AccLoanVO> queryListAll() {
		logger.info("获取未结清的台帐信息!");
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryListAll();
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public AccLoanVO queryByfirstDisbDate(AccLoanVO inVo) {
		logger.info("当前查询参数信息为:" + inVo);
		try {
			AccLoan queryAccLoan = new AccLoan();
			beanCopy(inVo, queryAccLoan);
			AccLoan queryRslAccLoan = accLoanDao.queryByfirstDisbDate(queryAccLoan);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.info("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.info("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public List<AccLoanVO> queryAccLoanListByCondition(AccLoanVO accLoanVO) {
		logger.info("条件查询台账列表，入参：" + JSONObject.toJSONString(accLoanVO));
		if (Objects.isNull(accLoanVO)) {
			return null;
		}
		try {
			List<AccLoan> dataList = accLoanDao.queryAccLoanListByCondition(accLoanVO);
			if (Objects.nonNull(dataList) && dataList.size() > 0) {
				List<AccLoanVO> resultList = new ArrayList<AccLoanVO>();
				resultList = (List<AccLoanVO>) beansCopy(dataList, AccLoanVO.class);
				return resultList;
			}
		} catch (Exception e) {
			logger.error("条件查询台账列表出现异常！", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据--风险分类调整按借据
	 */
	@Override
	public List<AccLoanVO> queryByRscOwner(AccLoanVO accLoanVo) {
		logger.info("查询用户未结清借据进行查询参数为:" + JSONObject.toJSONString(accLoanVo));
		List<AccLoanVO> lists = null;
		try {
			List<AccLoan> list = accLoanDao.queryByRscOwnerByPage(accLoanVo);
			overdueDays(list);
			logger.info("查询未结清借据数量为:" + list.size());
			pageSet(list, accLoanVo);
			lists = (List<AccLoanVO>) beansCopy(list, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return lists;
	}

	/**
	 * 查询当前机构权限数据--风险分类调整按借据
	 */
	@Override
	public List<AccLoanVO> queryByRscCurrOrg(AccLoanVO accLoanVo) {
		logger.info("查询当前机构未结清借据进行查询参数为:" + JSONObject.toJSONString(accLoanVo));
		List<AccLoanVO> lists = null;
		try {
			List<AccLoan> list = accLoanDao.queryByRscCurrOrgByPage(accLoanVo);
			overdueDays(list);
			logger.info("查询未结清借据数量为:" + list.size());
			pageSet(list, accLoanVo);
			lists = (List<AccLoanVO>) beansCopy(list, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return lists;
	}

	/**
	 * 查询当前机构及下属机构权限数据--风险分类调整按借据
	 */
	@Override
	public List<AccLoanVO> queryByRscCurrDownOrg(AccLoanVO accLoanVo) {
		logger.info("查询当前机构及下属机构未结清借据进行查询参数为:" + JSONObject.toJSONString(accLoanVo));
		List<AccLoanVO> lists = null;
		try {
			List<AccLoan> list = accLoanDao.queryByRscCurrDownOrgByPage(accLoanVo);
			overdueDays(list);
			logger.info("查询未结清借据数量为:" + list.size());
			pageSet(list, accLoanVo);
			lists = (List<AccLoanVO>) beansCopy(list, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return lists;
	}

	// 未结清借据列表
	@Override
	public List<String> queryBillListByAccStatus(AccLoanVO accLoanVo) {
		AccLoan AccLoan = new AccLoan();
		beanCopy(accLoanVo, AccLoan);
		List<String> billList = accLoanDao.queryBillListByAccStatus(AccLoan);
		logger.debug("当前查询未结清合同号的结果集数量为:" + billList.size());
		return billList;
	}

	@Override
	public List<AccLoanVO> queryListByStatus(List<String> billNoList) {
		logger.info("获取未结清的台帐信息!");
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryListByStatus(billNoList);
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<AccLoanVO> queryListByOvertime(List<String> billNoList) {
		logger.info("获取逾期的台帐信息!");
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryListByOvertimes(billNoList);
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	@Override
	public List<AccLoanVO> queryByBillNos(List<String> billNos) {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryByBillNos(billNos);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + accLoans.size());
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public AccLoanVO queryMinByCapOverdueDate(List<String> billNoList) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		AccLoanVO accLoanVO = null;
		try {
			AccLoan accLoan = accLoanDao.queryMinByCapOverdueDate(billNoList);
			logger.debug("当前查询本人所属数据信息的结果为:" + accLoan);
			if (Objects.nonNull(accLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(accLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return null;

	}

	@Override
	public AccLoanVO queryMinByIntOverdueDate(List<String> billNoList) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		AccLoanVO accLoanVO = null;
		try {
			AccLoan accLoan = accLoanDao.queryMinByIntOverdueDate(billNoList);
			logger.debug("当前查询本人所属数据信息的结果为:" + accLoan);
			if (Objects.nonNull(accLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(accLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return null;

	}

	@Override
	public AccLoanVO queryFirstByMaxTimesTotal(List<String> billNoList) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		AccLoanVO accLoanVO = null;
		try {
			AccLoan accLoan = accLoanDao.queryFirstByMaxTimesTotal(billNoList);
			logger.debug("当前查询本人所属数据信息的结果为:" + accLoan);
			if (Objects.nonNull(accLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(accLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return null;

	}

	@Override
	public AccLoanVO queryFirstBySevenResult(List<String> billNoList) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		AccLoanVO accLoanVO = null;
		try {
			AccLoan accLoan = accLoanDao.queryFirstBySevenResult(billNoList);
			logger.debug("当前查询本人所属数据信息的结果为:" + accLoan);
			if (Objects.nonNull(accLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(accLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return null;

	}

	@Override
	public List<AccLoanVO> queryLegalForOutWithStatus(List<String> billNos) {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<AccLoanVO> accLoanVO = new ArrayList<>();
		try {
			List<AccLoan> accLoan = accLoanDao.queryLegalForOutWithStatus(billNos);
			logger.debug("法务出催的结果为:" + accLoan);
			accLoanVO = (List<AccLoanVO>) beansCopy(accLoan, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return accLoanVO;
	}

	@Override
	public List<AccLoanVO> queryLegalForOutWithOverTime(List<String> billNos) {
		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<AccLoanVO> accLoanVO = new ArrayList<>();
		try {
			List<AccLoan> accLoan = accLoanDao.queryLegalForOutWithOverTime(billNos);
			logger.debug("法务出催的结果为:" + accLoan);
			accLoanVO = (List<AccLoanVO>) beansCopy(accLoan, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return accLoanVO;
	}

	@Override
	public List<AccLoanVO> queryAllAccLoan() {
		logger.info("开始查询贷款台账所有数据");
		List<AccLoanVO> accLoanVO = new ArrayList<AccLoanVO>();
		try {
			List<AccLoan> accLoanList = accLoanDao.queryAllAccLoan();
			logger.info("共：" + accLoanList.size() + "条");
			accLoanVO = (List<AccLoanVO>) beansCopy(accLoanList, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return accLoanVO;
	}

	@Override
	public BigDecimal queryPrdBalance(String prdCode, String legalOrgCode) {
		AccLoan accLoan = new AccLoan();
		accLoan.setLegalOrgCode(legalOrgCode);
		accLoan.setPrdCode(prdCode);
		try {
			logger.info("查询目前下放款额度法人机构号为: " + legalOrgCode + "，产品代码为：" + prdCode);
			BigDecimal loanBalance = accLoanDao.queryLoanBalanceByLegalOrgCode(accLoan);
			logger.info("查询法人机构: " + legalOrgCode + " 下的目前放款额度为: " + loanBalance);
			return loanBalance;
		} catch (Exception e) {
			logger.error("查询当前放款总额度出错", e);
		}
		return null;
	}

	@Override
	public List<AccLoanVO> queryBadResultByContNo(String contNo) {
		logger.info("查询合同底下所属借据信息的参数信息为:" + contNo);
		List<AccLoanVO> lists = null;
		try {
			List<AccLoan> list = accLoanDao.queryBadResultByContNo(contNo);
			overdueDays(list);
			logger.info("查询合同底下所属借据信息数量为:" + list.size());
			lists = (List<AccLoanVO>) beansCopy(list, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return lists;
	}

	@Override
	public List<String> queryCusIds4SevenClassify() {
		return accLoanDao.queryCusIds4SevenClassify();
	}

	@Override
	public List<AccLoanVO> queryAccLoanList4SevenClassifyByCusIds(List<String> cusIds) {
		if (Objects.isNull(cusIds) || cusIds.size() == 0) {
			return null;
		}
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryAccLoanList4SevenClassifyByCusIds(cusIds);
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	/**
	 *
	 * 根據马上同步过来的临时表数据修改台账表
	 *
	 * @param taskNo
	 * @return
	 * @throws PauseException
	 * @date:2018年12月25日 下午3:35:23
	 * @author:cenyj
	 */
	@Override
	public void batchUpdateAccLoanFromMaTxt() {

	}

	@Override
	public AccLoanVO queryBRR(AccLoanVO accLoanVO) {
		logger.debug("当前查询本人所属数据信息的参数信息为:" + accLoanVO);
		try {
			AccLoan accLoan = new AccLoan();
			beanCopy(accLoanVO,accLoan);
			AccLoan queryRslAccLoan = accLoanDao.queryBRR(accLoan);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 日初借据信息临时表数据到借据信息表有数据则更新，无则新增 <br>
	 * 0.1: weijj:2019年5月11日 下午2:29:59:新增 <br>
	 * @author  weijj
	 * @since 2019年5月11日 下午2:29:59
	 * @date 2019年5月11日 下午2:29:59
	 * @version 0.1
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertOrUpdateAccLoan(List<AccLoanVO> list) {
		// TODO Auto-generated method stub
		LocalDate today = LocalDate.now();
		String msg1 = "【借据信息表】数据同步开始..." + today;
		logger.info(msg1, "message{}");
		List<AccLoan> accLoanlList = new ArrayList<AccLoan>();
		int num = 0;
		try {
			accLoanlList = (List<AccLoan>) beansCopy(list, AccLoan.class);
			num = accLoanDao.insertOrUpdateAccLoan(accLoanlList);
			String num_msg = "【借据信息表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【借据信息表】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}

	/**
	 * 需求编号：【额度管控】
	 * 问题编号：【暂无】
	 * 开发人员：【chenxuan】
	 * 创建/修改日期：【2019/8/14】
	 * 修改描述：根据身份证、证件类型查询借据余额
	 * @Param
	 * @return
	 **/
	@Override
	public BigDecimal queryPreOccAmt(AccLoanVO accLoanVO) {
		logger.debug("查询借据余额开始:" + accLoanVO.toString());
		try {
			AccLoan accLoan = new AccLoan();
			beanCopy(accLoanVO,accLoan);
			BigDecimal loanBalance = accLoanDao.queryPreOccAmt(accLoan);
			if(loanBalance == null){
				loanBalance = BigDecimal.ZERO;
			}
			logger.debug(accLoan.getCertCode()+"查询借据余额结束，借据余额为:" + loanBalance.toString());
			return loanBalance;
		} catch (Exception e) {
			logger.error("查询借据余额发生异常!", e);
		}
		return null;
	}

    /**
     * 状态新增了 14-非应计
     * @param accLoanVO
     * @return
     */
    @Override
    public BigDecimal queryPreOccAmt2(AccLoanVO accLoanVO) {
        logger.debug("查询借据余额开始:" + accLoanVO.toString());
        try {
            AccLoan accLoan = new AccLoan();
            beanCopy(accLoanVO,accLoan);
            BigDecimal loanBalance = accLoanDao.queryPreOccAmt2(accLoan);
            if(loanBalance == null){
                loanBalance = BigDecimal.ZERO;
            }
            logger.debug(accLoan.getCertCode()+"查询借据余额结束，借据余额为:" + loanBalance.toString());
            return loanBalance;
        } catch (Exception e) {
            logger.error("查询借据余额发生异常!", e);
        }
        return null;
    }

	/**
	 * 需求编号：【额度管控】
	 * 问题编号：【暂无】
	 * 开发人员：【chenxuan】
	 * 创建/修改日期：【2019/8/14】
	 * 修改描述：
	 * @Param [paramMap]
	 * @return java.lang.String
	 **/
	@Override
	public String queryMaxBizdate(Map<String,String> paramMap){
		logger.debug("根据身份证、证件类型查询账务日期开始:" + paramMap.toString());
		try {
			String bizdate = accLoanDao.queryMaxBizdate(paramMap);
			logger.debug(paramMap.get("certCode")+"查询账务日期为:" + bizdate);
			return bizdate;
		} catch (Exception e) {
			logger.error("根据身份证、证件类型查询账务日期异常!", e);
		}
		return null;
	}

	/**
	 * create by luzy
	 * 问题编号：<br/>
	 * 功能描述：根据证件类型和证件号码查询借据表<br/>
	 * 创建时间：2019年8月19日<br/>
	 * @param accLoanVO
	 * @return
	 */
	@Override
	public List<AccLoanVO> queryAccLoan(AccLoanVO accLoanVO) {
		logger.debug("通过条件查询借据信息参数为:");
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryAccLoanByPage(accLoanVO);
			logger.debug("通过条件查询借据信息的结果集数量为:" + accLoans.size());
			pageSet(accLoans, accLoanVO);
			list = (List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public AccLoanVO queryOneAccLoanData(String applySeq) {
		logger.debug("当前查询本人所属数据信息的参数信息:" + applySeq);
		try {
			AccLoan queryRslAccLoan = accLoanDao.queryOneAccLoanData(applySeq);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public AccLoanVO queryDbllInfo(String billNo) {
		logger.debug("当前查询本人所属数据信息的参数信息:" + billNo);
		try {
			AccLoan queryRslAccLoan = accLoanDao.queryDbllInfo(billNo);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 根据证件类型、证件号码批量查询借据信息
	 * 开发人员：dengqiwen
	 * 创建/修改日期：【2020/03/17】
	 **/
	@Override
	public List<AccLoanVO> batchQuerybyCert(List<String> certTypes, List<String>certCodes) {
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accloanList = accLoanDao.batchQuerybyCert(certTypes,certCodes);
			list = (List<AccLoanVO>) beansCopy(accloanList, AccLoanVO.class);
		} catch (Exception e) {
			logger.error("【名单系统命中案例数据】查询借据信息出现异常!", e);
		}
		return list;
	}


	@Override
	public List<AccLoanVO> queryPersonlLoanInfo(AccLoanVO accLoanVO) {
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryPersonlLoanInfo(accLoanVO);
			list=(List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("个人借据收据查询beanscopy时候发生异常!", e);
		}
		return list;
	}

	@Override
	public AccLoanVO querySinglePersonlLoanInfo(AccLoanVO accLoanVO) {
		AccLoan accLoan = accLoanDao.querySinglePersonlLoanInfo(accLoanVO);
		return accLoan!=null?beanCopy(accLoan, new AccLoanVO()):null;
	}

	@Override
	public List<AccLoanVO> queryLoanInfoBybizDate(String openDay) {
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryLoanInfoByBizDate(openDay);
			list=(List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("根据bizDate查询借据拉列表发生异常!", e);
		}
		return list;	}


	@Override
	public List<AccLoanVO> queryByApplySqes(List<String> ids) {
		List<AccLoanVO> list = null;
		try {
			List<AccLoan> accLoans = accLoanDao.queryByApplySqes( ids);
			list=(List<AccLoanVO>) beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("根据bizDate查询借据拉列表发生异常!", e);
		}
		return list;
	}


	@Override
	public int insertOrUpdateByPk(AccLoanVO accLoanVO) {
		logger.debug("当前修改数据为:" + accLoanVO.toString());
		int num = 0;
		try {
			AccLoan accLoan = new AccLoan();
			beanCopy(accLoanVO, accLoan);
			num = accLoanDao.insertOrUpdateByPk(accLoan);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + accLoanVO + "修改的数据条数为" + num);
		return num;
	}

	@Override
	public List<AccLoanStatisticalInfo> queryAccLoanStatisticalInfos(List<String> billNos) {
		return this.accLoanDao.queryAccLoanStatisticalInfos(billNos);
	}


	@Override
    public int insertOrUpdatePsd(List<AccLoanVO> list) throws Exception {
            return accLoanDao.insertOrUpdatePsd((List<AccLoan>) beansCopy(list, AccLoan.class));
    }

	@Override
	public boolean isCusLossCredit(String cusId) {
		int result = accLoanDao.countLossCreditByCusId(cusId);
		if (result > 0) return true;
		else return false;
	}


	@Override
	public List<AccLoanVO> queryByCertCodeAndPrdId(String certCode, String prdId) throws Exception {
		return (List<AccLoanVO>) beansCopy(this.accLoanDao.queryByCertCodeAndPrdId(certCode, prdId), AccLoanVO.class);
	}



	@Override
	public List<AccLoanVO> queryLoanByCertCodeAndStatusAndAssure(String certCode, String certType) throws BizException {
		logger.debug("当前查询的身份证为:" + certCode+"证件类型为:"+certType);
		List<AccLoanVO> list = null;

		try {
			List<AccLoan> accLoanlist = accLoanDao.queryLoanByCertCodeAndStatusAndAssure(certCode, certType);
			list = (List<AccLoanVO>) beansCopy(accLoanlist, AccLoanVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("通过证件类型和证件号码查询出现异常", e);
			throw new BizException(e);
		}
		return list;
	}

	@Override
	public String queryPrdId(String billNo){
		logger.debug("当前查询的借据号为：" + billNo);
		try{
			String prdId = accLoanDao.queryPrdId(billNo);
			if (!StringUtil.isNullorBank(prdId)){
				return prdId;
			}
		} catch (Exception e){
			e.printStackTrace();
			logger.error("通过借据号查询出现异常");
		}
		return null;
	}


	@Override
	public List<AccLoanVO> queryByCertCode(String certCode) throws Exception {
		return (List<AccLoanVO>)beansCopy(this.accLoanDao.queryByCertCode(certCode), AccLoanVO.class);
	}


	@Override
	public AccLoanVO queryLSAndDNByPK(String billNo) {
		logger.debug("当前查询本人所属数据信息的参数信息:" + billNo);
		try {
			AccLoan queryRslAccLoan = accLoanDao.queryLSAndDNByPK(billNo);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 根据证件类型、证件号码批量查询当前逾期状态
	 * 开发人员：dengqiwen
	 * 创建/修改日期：【2020/05/14】
	 **/
	@Override
	public int batchInsert(List<AccLoanVO> list) throws Exception {
		logger.debug("批量新增acc_loan贷款台账表,当前修改数据为:" + list.size() + "条");
		List<AccLoan> accLoanList = new ArrayList<AccLoan>();
		int num = 0;
		try {
			accLoanList =(List<AccLoan>) beansCopy(list, AccLoan.class);
			num = accLoanDao.batchInsert(accLoanList);
		} catch (Exception e) {
			logger.error("批量新增acc_loan贷款台账表发生异常!", e);
			num = -1;
		}
		logger.debug("修改的数据条数为" + num);
		return num;
	}

	public AccLoanVO queryOverdue(String certType, String certCode) throws Exception{
		logger.info("查询网贷当前逾期状态certCode:"+certCode);
		AccLoan queryAccLoan=accLoanDao.queryOverdue( certType, certCode);
		AccLoanVO outAccLoanVo = beanCopy(queryAccLoan, new AccLoanVO());
		return outAccLoanVo;
	}

	/**
	 * 根据证件号码、证件类型、产品编码 查询借据清单
	 *
	 * @author xieziwei
	 */
	public List<AccLoanVO> queryByCertandPrd(AccLoanVO accLoanVO){
		logger.info("根据证件号查询借据数据开始"+accLoanVO.getCertCode());
		AccLoan accLoan = beanCopy(accLoanVO, new AccLoan());
		List<AccLoan> accLoans = accLoanDao.queryByCertandPrd(accLoan);
		List<AccLoanVO> list  = null;
		try {
			list  = (List<AccLoanVO>)beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<AccLoanVO> queryByCertAndPrdByPage(AccLoanVO accLoanVO){
		logger.info("根据证件号查询借据数据开始"+accLoanVO.getCertCode());
		AccLoan accLoan = beanCopy(accLoanVO, new AccLoan());
		List<AccLoan> accLoans = accLoanDao.queryByCertAndPrdByPage(accLoan);
		List<AccLoanVO> list  = null;
		try {
			list  = (List<AccLoanVO>)beansCopy(accLoans, AccLoanVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

    @Override
    public AccLoanVO queryByPrdIdAndApplySeq(AccLoanVO accLoanVo) {
        logger.debug("当前查询参数信息为:" + accLoanVo);
        try {
            AccLoan queryAccLoan = new AccLoan();
            beanCopy(accLoanVo, queryAccLoan);
            AccLoan queryRslAccLoan = accLoanDao.queryByPrdIdAndApplySeq(queryAccLoan);
            if (Objects.nonNull(queryRslAccLoan)) {
                AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
                logger.debug("当前查询结果为:" + outAccLoanVo.toString());
                return outAccLoanVo;
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }
	@Override
	public int insertOrUpdateYed(List<AccLoanVO> list) throws Exception {
		return accLoanDao.insertOrUpdatePsd((List<AccLoan>) beansCopy(list, AccLoan.class));
	}

	@Override
	public AccLoanVO queryByApplySeq(AccLoanVO accLoanVo) {
		logger.debug("当前查询参数信息为:" + accLoanVo);
		try {
			AccLoan queryAccLoan = new AccLoan();
			beanCopy(accLoanVo, queryAccLoan);
			AccLoan queryRslAccLoan = accLoanDao.queryByApplySeq(queryAccLoan);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.debug("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public AccLoanVO queryfirstDisbDateByCond(AccLoanVO inVo) {
		logger.info("当前查询参数信息为:" + inVo);
		try {
			AccLoan queryAccLoan = new AccLoan();
			beanCopy(inVo, queryAccLoan);
			AccLoan queryRslAccLoan = accLoanDao.queryfirstDisbDateByCond(queryAccLoan);
			if (Objects.nonNull(queryRslAccLoan)) {
				AccLoanVO outAccLoanVo = beanCopy(queryRslAccLoan, new AccLoanVO());
				logger.info("当前查询结果为:" + outAccLoanVo.toString());
				return outAccLoanVo;
			} else {
				logger.info("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
}